<template lang="pug">
    div(class="statistics")
        skin(@up="up",@down="down",@yes="yes",@back="back")
            div(class="flex-column h2")
                div(class="flex-column-item flex-row")
                    div(class="flex-row-item flex-3 center") {{current | format('YYYY/MM/DD')}}
                div(class="flex-column-item flex-row")
                    div(class="flex-row-item flex-3") 当日已输注
                    div(class="flex-row-item right") {{usedAmount}}u
                div(class="flex-column-item flex-row")
                    div(class="flex-row-item flex-3") 上次大剂量：
                    div(class="flex-row-item right") {{dayDoseLimit.value}}u
                div(class="flex-column-item flex-row")
                    div(class="flex-row-item flex-2") {{record.date |  format("MMDD")}}
                    div(class="flex-row-item center flex-2")
                        span {{record.date| format("HH:mm")}}
                        span >
                        span {{record.amount}}
                    div(class="flex-row-item right flex-1") u
</template>

<script>
    import dayjs from 'dayjs';
    import Base from '@/mixins/BaseVue';

    export default {
        mixins:[Base],
        data() {
            return {
                record: [].concat(window.dose.records).reverse()[0],
                dayDoseLimit: window.advance.dayDoseLimit,
                current: dayjs().add(window.machine.timeDiff || 0 , "second"),
                interval: null
            };
        },
        computed: {
            usedAmount() {
                let result = 0;
                const today = dayjs().format("YYYYMMDD")
                window.dose.records.forEach(item => {
                    if (dayjs(item.date).format("YYYYMMDD") == today) result = (result * 10 + item.amount * 10) / 10;
                })
                return result;
            }
        },
        methods: {
            up() {
                clearInterval(this.interval)
                this.$router.push('/home/mst');
            },
            down() {
                clearInterval(this.interval)
                this.$router.push('/home/mst');
            },

            back() {
                clearInterval(this.interval)
                this.$router.push('/index');
            },
            yes() {
                clearInterval(this.interval)
                this.$router.push('/index');
            },
            getCurrent() {
                this.current = dayjs().add(-window.machine.timeDiff || 0, 'seconds');
            }
        },
        created() {
            this.getCurrent();
            this.interval = setInterval(function () {
                this.getCurrent();
            }.bind(this), 1000)
        }
    };
</script>
